import { Alert, Table } from 'antd';
import React, { Component, Fragment } from 'react';
import styles from './index.less';

class StandardTable extends Component {
  constructor(props) {
    super(props);

    this.state = {
      // selectedRowKeys: [],
    };
  }

  onPageChange = (pagination, filters, sorter, ...rest) => {
    const { onChange, accessor } = this.props;

    if (onChange) {
      onChange(pagination, filters, sorter, ...rest);
    } else if (accessor) {
      accessor({
        current: pagination.current
      });
    }
  };

  render () {
    let { data, paging, accessor, model, ...rest } = this.props;

    // 向前兼容：如果有 model, 则可以不关心 data 和 paging
    if (model) {
      data = model.list;
      paging = model.paging;
    }

    // 分页参数
    const paginationProps = paging // total pageSize current
      ? {
        defaultCurrent: 1,
        defaultPageSize: 10,

        ...paging,
      }
      : false;

    return (
      <div className={styles.standardTable}>
        <Table
          rowKey={record => record.id}
          dataSource={data}
          pagination={paginationProps}
          onChange={this.onPageChange}
          {...rest}
        />
      </div>
    );
  }
}

export default StandardTable;
